#include "drawcurvewindow.h"
#include "ui_drawcurvewindow.h"

DrawCurveWindow::DrawCurveWindow(DealPanData *d, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DrawCurveWindow)
{
    dpd = d;
    ui->setupUi(this);
}

DrawCurveWindow::~DrawCurveWindow()
{
    delete ui;
}


void DrawCurveWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    PaintCurve* pcImage = new PaintCurve(this->width(), this->height(), dpd);
    image = pcImage->image;
    painter.drawImage(0,0,image);
    delete pcImage;
    //qDebug() << this->width() << " " << this->height();
}

void DrawCurveWindow::on_actionSave_triggered()
{
    QString imageName = QFileDialog::getSaveFileName(this, tr("Save File"),
                               "untitled.png",
                               tr("Images (*.png *.jpg)"));
    if(imageName.isEmpty())
    {
        return;
    }
    QFileInfo fi(imageName);
    QString ext = fi.suffix();
    if(ext == "png" || ext == "jpg")
    {
        if(ext == "jpg")
        {
            image.save(imageName, "jpg");
        }else
        {
            image.save(imageName);
        }
    }else
    {
        QMessageBox::warning(this, "Fail to Save", "Suffix should be png or jpg", QMessageBox::Ok);
    }
}
